#include "fileutiltest.h"
#include <filesystem>
#include <fstream>

#ifdef Q_OS_WIN
#include <windows.h>
#endif

#include <common/logger/logger.h>

FileUtilTest::FileUtilTest(QObject* parent)
{
}
void FileUtilTest::initTestCase()
{
}
void FileUtilTest::tstFileUtil()
{
	// 测试压缩文件
	std::string filePath = "test.txt";
	std::string zipPath = "test.zip";
	std::ofstream(filePath).put('A');// 创建简单测试文件

	bool ok = FileUtil::zipFile(filePath, zipPath);
	QVERIFY(ok);// QtTest 断言

	// 测试解压文件
	std::string outDir = "out";
	std::filesystem::create_directory(outDir);
	ok = FileUtil::unZipFile(zipPath, outDir);
	QVERIFY(ok);

	// 测试文件内容
	auto bytes = FileUtil::getFileBytes(outDir + "/test.txt");
	QCOMPARE(bytes.size(), size_t(1));
	QCOMPARE(bytes[0], static_cast<unsigned char>('A'));

	/// ========================================================
	//  准备测试图片（创建一个1x1的红色PNG）
	std::string testImagePath = "test_image.png";
	QImage testImage(1, 1, QImage::Format_RGB32);
	testImage.fill(Qt::red);
	testImage.save(QString::fromStdString(testImagePath), "PNG");

	// 测试：读取有效图片
	QImage image = FileUtil::getImage(testImagePath);
	QCOMPARE(image.pixelColor(0, 0), QColor(Qt::red));// 颜色正确
													  // 测试：读取.txt系统图标
	//测试：读取.txt系统文件图标
	QImage systemImage = FileUtil::getFileIcon(filePath);
	QVERIFY(!systemImage.isNull());
	systemImage.save("txtSystemIcon.png");
	
	//测试：读取url系统文件图标
	std::string urlPath = "http://i-blog.csdnimg.cn/blog_migrate/258099b2c8b250572d7d9599aebb4398.png";
	QImage urlImage = FileUtil::getFileIcon(urlPath);
	QVERIFY(!urlImage.isNull());
	urlImage.save("urlIcon.png");
}
void FileUtilTest::cleanupTestCase()
{
}

int main(int argc, char** argv)
{

	// 设置Windows控制台编码
#ifdef Q_OS_WIN
	// 设置控制台输出代码页为 UTF-8
	SetConsoleOutputCP(CP_UTF8);
	// 可选：设置控制台输入为 UTF-8
	SetConsoleCP(CP_UTF8);
#endif

	QApplication app(argc, argv);
	FileUtilTest tc;
	return QTest::qExec(&tc, argc, argv);// qExec 会把失败信息打印到 stdout
}